package com.stage.battle
{
	import com.ui.layer.BattleLayer;
	
	import flash.display.Sprite;
	
	import mx.core.Application;

	public class BattleTextManager
	{
		private var _flowTextNum:int = 10;
		private var _flowTextArr:Array = []; // 浮动字符的池
		private var _flowDataList:Array = [];
		private var _container:Sprite;
		private var _hitsText:HitsText = new HitsText;
		
		private static var _instance:BattleTextManager;
		public static function getInstance():BattleTextManager{
			if(!_instance){
				_instance = new BattleTextManager;
			}
			return _instance;
		}
		
		public function BattleTextManager()
		{
			for(var i:int=0; i<_flowTextNum; i++){
				var flowText:FlowText = new FlowText;
				flowText.callback = showNext;
				_flowTextArr.push(flowText);
			}
		}
		
		public function set container(con:Sprite):void{
			_container = con;
			for(var i:int=0; i<_flowTextNum; i++){
				var flowText:FlowText = _flowTextArr[i] as FlowText;
				if(flowText.parent){
					flowText.parent.removeChild(flowText);
				}
				if(_container){
					_container.addChild(flowText);
				}
			}
		}
		
		private function showNext():void{
			var index:int = 0;
			while(_flowDataList.length > 0 && index < _flowTextNum){
				for(var i:int=index; i<_flowTextNum; i++, index++){
					var flowText:FlowText = _flowTextArr[i] as FlowText;
					if(flowText.isFree){
						var data:Object = _flowDataList.shift();
						if(data.func == "show"){
							flowText.x = data.x;
							flowText.y = data.y;
							flowText.show(data.color, data.value, data.flag);
						}else if(data.func == "showText"){
							flowText.x = data.x;
							flowText.y = data.y;
							flowText.showText(data.color, data.value);
						}
						break;
					}
				}
			}
		}
		
		public function showFlowText(color:String, value:String, x:Number, y:Number):void{
			var data:Object = {func:"showText", color:color, value:value, x:x, y:y};
			_flowDataList.push(data);
			showNext();
		}
		
		public function showFlowNum(color:String, value:int, x:Number, y:Number, flag:Boolean = false):void{
			var data:Object = {func:"show", color:color, value:value, x:x, y:y, flag:flag};
			_flowDataList.push(data);
			showNext();
		}
		
		public function showHitsNum(color:String, value:int):void{
			if(!_hitsText.parent){
				Application.application.gameUI.battleLayer.container.addChild(_hitsText);	
			}
			_hitsText.x = _container.stage.stageWidth - 200;
			_hitsText.y = 50;
			_hitsText.show(color, value);
		}
	}
}